<?php
require_once("../../resources/commons.php");
require_once("../../data/User.php");
require_once("HTML/QuickForm/Controller.php");
require_once("HTML/QuickForm/Action/Display.php");
require_once("HTML/QuickForm/Action/Submit.php");
require_once("HTML/QuickForm/Text.php");
require_once("HTML/QuickForm/Password.php");
require_once("HTML/QuickForm/Submit.php");
require_once("HTML/QuickForm/Select.php");

/**
 * Profile form.<br/>
 * Form to be used by a user to update his/her own profile.
 */
class ProfilePage extends HTML_QuickForm_Page{
    function buildForm(){
        $this->_formBuilt = true;

		$l_firstNameInput=new HTML_QuickForm_text("firstName");
		$l_firstNameInput->setLabel("First name:");
		$this->addElement($l_firstNameInput);
		$l_lastNameInput=new HTML_QuickForm_text("lastName");
		$l_lastNameInput->setLabel("Last name:");
		$this->addElement($l_lastNameInput);
		
		
		$l_managerSelect=new HTML_QuickForm_select("manager");
		$l_managerSelect->setLabel("Select your manager:");
		$l_records=User::getFromCustomCriteria(
			"WHERE isActive=:isActive and not id=:id",
			array("isActive" => true, "id" => SessionManager::getCurrentLoggedUserId()));
		$l_managerSelect->addOption("No manager","null");
		foreach($l_records as $l_user){
			$l_managerSelect->addOption($l_user->getFullName(),base64_encode($l_user->getId()));
		}
		$this->addElement($l_managerSelect);
		
		
		
		$l_submit=new HTML_QuickForm_submit("submit");
		$l_submit->setValue("OK");
		$this->addElement($l_submit);
		
		//The validation rules (either client and server)
		$this->addRule("firstName","First name must not be empty","required",null,"client");
		$this->addRule("lastName","Last name must not be empty","required",null,"client");
		$this->addRule("firstName","First name must not be empty","required",null,"server");
		$this->addRule("lastName","Last name must not be empty","required",null,"server");
		$this->addRule("manager","Manager must not be empty","required",null,"server");
		
		$this->applyFilter("firstName","trim");
		$this->applyFilter("lastName","trim");
		
        $this->setDefaultAction("submit");
    }
}

/**
 * We subclass the default 'display' handler to customize the output
 */
class ActionDisplay extends HTML_QuickForm_Action_Display{
    function _renderForm($page){   	
    	SessionManager::getInstance()->checkSession();
    	$l_user=User::getFromPrimaryKey(SessionManager::getInstance()->getCurrentLoggedUserId());
    	$page->setDefaults(array(
    		"firstName" => $l_user->getFirstName(),
    		"lastName" => $l_user->getLastName(),
    		"manager" => base64_encode($l_user->getManagerUser_id())
    	));
    	
    	displayTop("Profile management",null,null);
        $page->display();
        displayBottom();
    }
}

/**
 * Action to be called after a submit action.<br>
 * Send an invitation to the user with a link
 * to be used to access the registration screen.
 */
class ActionProcess extends HTML_QuickForm_Action{
    function perform($page){
    	SessionManager::getInstance()->checkSession();
    	
    	$l_user=User::getFromPrimaryKey(SessionManager::getInstance()->getCurrentLoggedUserId());
    	$l_user->setFirstName($page->exportValue("firstName"));
    	$l_user->setLastName($page->exportValue("lastName"));
    	
    	$l_manager=$page->exportValue("manager");
    	if($l_manager == "null"){
    		$l_user->setManagerUser_id(null);
    	}else{
    		$l_user->setManagerUser_id(base64_decode($l_manager));
    	}
    	$l_user->save();
    	
    	HTTP::redirect("index.php");
    }
}

$controller =new HTML_QuickForm_Controller('ProfileManagementProcess', false);
$controller->addPage(new ProfilePage('profileUpdate'));
$controller->addAction('display', new ActionDisplay());
$controller->addAction('process', new ActionProcess());
$controller->addAction('submit', new HTML_QuickForm_Action_Submit());

$controller->run();

?>